package com.medeolinx.control;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.medeolinx.control.util.Constans;
import com.medeolinx.model.ModelManager;
import com.medeolinx.model.data.User;

/**
 * handle URL: login, signOut, signUp home. <br>
 * auto wired com.medeolinx.model.ModelManager with name modelManager
 * 
 */
@Controller
public class LoginController {

	private static class Links {
		static final String LOGIN = "/login";
		static final String HOME = "/home";
		static final String SIGNOUT = "/signOut";
		static final String SIGNUP = "/signUp";
	}

	private static class ViewPages {
		/** login page */
		static String REDIRECT_LOGIN = "redirect:/index.html";
		static String HOME = "home";
	}

	@Autowired(required = true)
	private ModelManager modelManager;

	/*
	 * @RequestMapping(value ="/login") public String
	 * login(@RequestParam("username") String username,
	 * 
	 * @RequestParam("password") String password, HttpSession session, Model
	 * model) { if (modelManager.validateUser(username, password)) { User user =
	 * new User(); user.setUsername(username);
	 * session.setAttribute(Constans.Session.USER, user);
	 * model.addAttribute(Constans.Session.MESSAGE, "Welcome "); return
	 * Constans.Session.MESSAGE; } else { return REDIRECT_LOGIN; } }
	 */

	@RequestMapping(value = Links.LOGIN)
	public String login(@ModelAttribute(Constans.Http.USER) User user, HttpSession session, HttpServletRequest request) {
		User u = modelManager.getUser(user.getName(), user.getPassword());
		if (u != null) {
			request.removeAttribute(Constans.Http.USER);
			session.setAttribute(Constans.Http.USER, u);
			return ViewPages.HOME;
		} else {
			return ViewPages.REDIRECT_LOGIN;
		}
	}

	@RequestMapping(value = Links.HOME)
	public String login(HttpSession session, Model model) {
		if (session.getAttribute(Constans.Http.USER) != null) {
			model.addAttribute(Constans.Http.MESSAGE, "Welcome ");
			return ViewPages.HOME;
		} else {
			return ViewPages.REDIRECT_LOGIN;
		}
	}

	@RequestMapping(value = Links.SIGNOUT)
	public String signOut(HttpSession session) {
		session.removeAttribute(Constans.Http.USER);
		return ViewPages.REDIRECT_LOGIN;
	}

	@RequestMapping(value = Links.SIGNUP)
	public String signUp(@ModelAttribute(Constans.Http.USER) User user, HttpSession session, Model model) {
		int id = -1;
		try {
			id = modelManager.addNewUser(user);
		} catch (Throwable e) {
			e.printStackTrace();
		}
		if (id != -1) {
			session.setAttribute(Constans.Http.USER, user);
			model.addAttribute(Constans.Http.MESSAGE, "Welcome ");
			return ViewPages.HOME;
		} else {
			return ViewPages.REDIRECT_LOGIN;
		}
	}

	// ///////////////////// POJO set/get methods ///////////////////////

	public ModelManager getModelManager() {
		return modelManager;
	}

	public void setModelManager(ModelManager modelManager) {
		this.modelManager = modelManager;
	}

}
